package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class UserDao {
private DBHelp<User> db = new DBHelp<User>();
	
	public User findByUserNameAndPassword(String name,String password) {
		String sql = "select id,username,password from t_user where username = ? and password = ?";
		return db.executeQueryForObject(sql, new UserRowMapper(), name,password);
	}
	
	private class UserRowMapper implements RowMapper<User>{

		public User mapperRow(ResultSet rs) throws SQLException {
			User user = new User();
			user.setId(rs.getInt("id"));
			user.setPassword(rs.getString("password"));
			user.setUsername(rs.getString("username"));
			return user;
		}
		
	}

	public void update(User user) {
		String sql = "update t_user set username = ?,password = ? where id = ?";
		db.executeSQL(sql, user.getUsername(),user.getPassword(),user.getId());
	}
}
